import { _decorator, Component, Layers, Node, resources, Sprite, SpriteFrame, UITransform } from 'cc'
import { TILE_CONFIGS } from '../utils/constants'
import { TILE_TYPE_ENUM } from '../enums'
const { ccclass, property } = _decorator
/** 瓦片地图生成逻辑 */
@ccclass('TileMgr')
export class TileMgr extends Component {
  type: TILE_TYPE_ENUM
  moveable: boolean // 角色可移动
  turnable: boolean // 角色可旋转
  /**
   * 初始化瓦片地图
   * @param tileType 瓦片类型
   * @param spriteFrame 精灵帧
   * @param columnIndex 列索引
   * @param rowIndex 行索引
   */
  async init(tileType: TILE_TYPE_ENUM, spriteFrame: SpriteFrame, columnIndex: number, rowIndex: number) {
    this.type = tileType
    // 根据瓦片类型判断当前瓦片是否可以移动和旋转
    if (
      this.type === TILE_TYPE_ENUM.WALL_LEFT_TOP ||
      this.type === TILE_TYPE_ENUM.WALL_ROW ||
      this.type === TILE_TYPE_ENUM.WALL_RIGHT_TOP ||
      this.type === TILE_TYPE_ENUM.WALL_LEFT_BOTTOM ||
      this.type === TILE_TYPE_ENUM.WALL_RIGHT_BOTTOM ||
      this.type === TILE_TYPE_ENUM.WALL_COLUMN
    ) {
      this.moveable = false
      this.turnable = false
    } else if (
      this.type === TILE_TYPE_ENUM.CLIFF_LEFT ||
      this.type === TILE_TYPE_ENUM.CLIFF_CENTER ||
      this.type === TILE_TYPE_ENUM.CLIFF_RIGHT
    ) {
      this.moveable = false
      this.turnable = true
    } else if (this.type === TILE_TYPE_ENUM.FLOOR) {
      this.moveable = true
      this.turnable = true
    }

    const sprite = this.addComponent(Sprite) // 添加精灵组件
    sprite.spriteFrame = spriteFrame // 设置精灵帧
    // 设置瓦片的大小
    const transform = this.getComponent(UITransform)
    transform.setContentSize(TILE_CONFIGS.TILE_WIDTH, TILE_CONFIGS.TILE_HEIGHT)
    // 设置瓦片的位置
    this.node.setPosition(columnIndex * TILE_CONFIGS.TILE_WIDTH, -(rowIndex * TILE_CONFIGS.TILE_HEIGHT))
  }
}
